package com.meusconcursos;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.Toast;

import com.meusconcursos.dto.ConteudoProgramatico;

public class ConteudoProgramaticoListActivity extends ListActivity {

	private class ConteudoProgramaticoAdapter extends
			ArrayAdapter<ConteudoProgramatico> {

		private ArrayList<ConteudoProgramatico> items;

		public ConteudoProgramaticoAdapter(Context context,
				int textViewResourceId, ArrayList<ConteudoProgramatico> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.list_item_conteudoprogramatico, null);
			}

			ConteudoProgramatico conteudoProgramatico = items.get(position);
			if (conteudoProgramatico != null) {
				CheckedTextView check = (CheckedTextView) v
						.findViewById(android.R.id.text1);
				check.setChecked(conteudoProgramatico.isChecked());
				check.setText(conteudoProgramatico.getDescricao());
			}

			return v;

		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(R.string.titulo_lista_conteudoprogramatico);
		setContentView(R.layout.list_conteudoprogramatico);

		ConteudoProgramaticoAdapter adapter = new ConteudoProgramaticoAdapter(
				this,
				R.layout.list_item_conteudoprogramatico,
				MeusConcursosActivity.dbHelper
						.getConteudoProgramaticoFromMateria((String) getIntent()
								.getExtras().get("id_materia")));
		setListAdapter(adapter);

		this.getListView().setOnItemClickListener(
				new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parentView,
							View childView, int position, long id) {

						CheckedTextView check = (CheckedTextView) childView;
						check.setChecked(!check.isChecked());
						ConteudoProgramatico conteudoProgramatico = (ConteudoProgramatico) parentView
								.getItemAtPosition(position);
						MeusConcursosActivity.dbHelper.configurarUnidadeEstudo(
								(String) getIntent().getExtras().get(
										"id_planoestudo"),
								conteudoProgramatico.getId(), check.isChecked());

					}

				});

		getListView().setOnItemLongClickListener(
				new AdapterView.OnItemLongClickListener() {

					@Override
					public boolean onItemLongClick(AdapterView<?> parentView,
							View arg1, int position, long arg3) {

						ConteudoProgramatico conteudoProgramatico = (ConteudoProgramatico) parentView
								.getItemAtPosition(position);

						Toast.makeText(getBaseContext(),
								conteudoProgramatico.getDescricao(),
								Toast.LENGTH_LONG).show();

						return false;
					}
				});

	}

}
